home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / blf082b / nlist.c < prev    next >
C/C++ Source or Header  |  1993-08-20  |  4KB  |  241 lines

  1. /*
  2.  *    NetList Compiler
  3.  *
  4.  *    Compiles a fidonet nodelist to a text list of nets
  5.  *    and a a string describing the net.
  6.  *
  7.  *    The format is:
  8.  *
  9.  *    <zone>:<net> name, town
  10.  *
  11.  *    Originally written for the BloufGate project by F Arnaud.
  12.  *    Public Domain. May be copied, sold, etc freely.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17. #include    <string.h>
  18. #include    <stdarg.h>
  19. #include    <stdlib.h>
  20. #include    <time.h>
  21. #include    <ctype.h>   
  22. #include    <errno.h>
  23.  
  24. #define        NLLEN    30
  25.  
  26. /*#define ATARIST*/
  27.  
  28. #ifdef ATARIST
  29. #include    <dos.h>
  30. #define     PATL    "nodelist.???"
  31. #endif
  32.  
  33. #define     SLIST_VERSION     "0.9.2"
  34.  
  35.  
  36. /* proto */
  37. int write_net(FILE *,int,int,char *);
  38. void usage(int);
  39. /* global */
  40. int uses_cities=0;
  41.  
  42.  
  43. void usage(int x)
  44. {
  45.     printf("usage: nlist [-c] <st-louis-nodelist> [netlist]\n\n");
  46.     exit(x);
  47. }
  48.  
  49. void main(int argc, char **argv)
  50. {
  51.     FILE *nl;
  52.     FILE *lzx;
  53.     int curzone, curnet,n,i;
  54.     char nlname[FILENAME_MAX],slxname[FILENAME_MAX];
  55.     char temp[200];
  56.     
  57. #ifdef ATARIST
  58.     struct FILEINFO inf;
  59. #endif
  60.     
  61.     printf("NetList Compiler version %s, Public Domain Apr 1992\n\n",SLIST_VERSION);
  62.  
  63.     strcpy(slxname,"netlist.blf");
  64.         
  65.     i=1; n=0;
  66.         
  67.     if(argc>i)
  68.     {
  69.         if(stricmp(argv[i],"-c")==0)
  70.         { 
  71.             i++; 
  72.             uses_cities=1;
  73.         }
  74.     }
  75.         
  76.     if(argc<=i)
  77.     {
  78. #ifdef ATARIST
  79.         *nlname=0;
  80.         if(!dfind(&inf,PATL,0))
  81.         {
  82.             do {
  83.                 if( isdigit(inf.name[strlen(PATL)-3])
  84.                         && isdigit(inf.name[strlen(PATL)-2])
  85.                         && isdigit(inf.name[strlen(PATL)-1]) )
  86.                     strcpy(nlname,inf.name);
  87.             } while(!dnext(&inf));
  88.         }    
  89.         
  90.         if(!*nlname)
  91.         {
  92.             printf("Can't find nodelist!\n");
  93.             usage(10);
  94.         }
  95. #else
  96.         /* fixme: doesn't work everywhere, should show the usage */
  97.         strcpy(nlname,"nodelist.*");
  98. #endif
  99.     }
  100.     else
  101.     {    
  102.         strcpy(nlname,argv[i++]);
  103.         if(argc>i)
  104.             strcpy(slxname,argv[i]);
  105.     }
  106.  
  107.     printf("Loading %s\n",nlname);
  108.     
  109.     nl=fopen(nlname,"r"); /* fixme ? */
  110.     if(!nl)
  111.     {
  112.         printf("Can't open nodelist.*\n");
  113.         usage(11);
  114.     }
  115.     else
  116.     {
  117.         lzx=fopen(slxname,"w");
  118.         if(!lzx)
  119.             printf("Can't open netlist output file\n");
  120.         else
  121.         {
  122.             time_t now;
  123.             /* print id */
  124.             now=time(NULL);
  125.             strftime(temp,200,"%d %b %y",localtime(&now));
  126.             fprintf(lzx,"#\n# %s compiled on %s with NetList Compiler vers. %s\n#\n",
  127.                     nlname,temp,SLIST_VERSION);
  128.             curzone=0;
  129.             curnet=0;
  130.             
  131.             while(fgets(temp,200,nl)!=NULL)
  132.             {
  133.                 if(*temp!=';')
  134.                 {
  135.                     if(*temp==',')
  136.                     {
  137.                         n++;
  138.                     }
  139.                     else if(strncmp(temp,"Zone,",5)==0)
  140.                     {
  141.                         curzone=atoi(temp+5);
  142.                         curnet=curzone;
  143.                         printf("Compiling Zone %d, Net %d\n",curzone,curnet);
  144.                         if(write_net(lzx,curzone,curnet,temp))
  145.                             break;
  146.                         n++;
  147.                     }
  148.                     else if(strncmp(temp,"Region,",7)==0)
  149.                     {
  150.                         curnet=atoi(temp+7);
  151.                         printf("Compiling Zone %d, Net %d\n",curzone,curnet);
  152.                         if(write_net(lzx,curzone,curnet,temp))
  153.                             break;
  154.                         n++;
  155.                     }
  156.                     else if(strncmp(temp,"Host,",5)==0)
  157.                     {
  158.                         curnet=atoi(temp+5);
  159.                         printf("Compiling Zone %d, Net %d\n",curzone,curnet);
  160.                         if(write_net(lzx,curzone,curnet,temp))
  161.                             break;
  162.                         n++;
  163.                     }
  164.                     else if( (strncmp(temp,"Pvt,",4)==0) || (strncmp(temp,"Hub,",4)==0) )
  165.                     {
  166.                         n++;
  167.                     }
  168.                     else if( (strncmp(temp,"Hold,",5)==0) || (strncmp(temp,"Down,",5)==0) )
  169.                     {
  170.                         n++;
  171.                     }
  172.                     else 
  173.                         printf("Bad keyword %s\n",temp);
  174.                 }
  175.             }
  176.             fclose(lzx);
  177.         }
  178.         fclose(nl);
  179.     }
  180.     printf("%d nodes processed.",n);
  181. }
  182.  
  183. int write_net(FILE *idx, int zone, int net, char *temp)
  184. {
  185.     char *ptr;
  186.     char t2[200];
  187.     
  188.     /* write node curzone, curnet, node */
  189.     if(!zone || !net)
  190.     {
  191.         printf("Zone or Net 0!\n");
  192.         return 1;
  193.     }
  194.     
  195.     /* <w>,<nb>,System,City,Don't_care => "System,City" */
  196.     ptr=strchr(temp,',');
  197.     if(!ptr)
  198.     {    
  199.         printf("invalid line %s\n",temp);
  200.         return 0;
  201.     }
  202.     ptr=strchr(++ptr,',');
  203.     if(!ptr)
  204.     {    
  205.         printf("invalid line %s\n",temp);
  206.         return 0;
  207.     }
  208.     
  209.     strcpy(t2,++ptr);
  210.     ptr=strchr(t2,',');
  211.     if(!ptr)
  212.     {
  213.         printf("invalid line %s\n",temp);
  214.         return 0;
  215.     }
  216.     
  217.     if(uses_cities)
  218.     {
  219.         ptr=strchr(++ptr,',');
  220.         if(!ptr)
  221.         {
  222.             printf("invalid line %s\n",temp);
  223.             return 0;
  224.         }
  225.     }
  226.     
  227.     *ptr=0; /* truncate the end of the line */
  228.     
  229.     /* _ to spaces */
  230.     ptr=t2;
  231.     while(*ptr++)
  232.         if(*ptr=='_')
  233.             *ptr=' ';
  234.  
  235.     if(strlen(t2)>=NLLEN)
  236.         t2[NLLEN-1]='\0';
  237.     
  238.     fprintf(idx,"%d:%d %s\n",zone,net,t2);
  239.     
  240.     return 0;
  241. }